package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.ToastUtils
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.ClassDetailAty
import hlhj.fhp.supreme.activitys.RedDetailAty
import hlhj.fhp.supreme.adapters.SpendHisAdp
import hlhj.fhp.supreme.javabean.SpendHisBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.rcylist.view.*

/**
 * Created by Never Fear   on 2018\7\26 0026.
Never More....
 */
class SpendHisFgm :Fragment(), SpringView.OnFreshListener {
    private lateinit var rootView:View
    private val datas=ArrayList<SpendHisBean.DataBean>()
    private lateinit var adp:SpendHisAdp
    companion object {
        fun getInstence(id:String,name:String): SpendHisFgm {
            val spendHisFgm = SpendHisFgm()
            val bundle = Bundle()
            bundle.putString("id",id)
            bundle.putString("name",name)
            spendHisFgm.arguments=bundle
            return spendHisFgm
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView = inflater?.inflate(R.layout.rcylist, null)

        adp= SpendHisAdp(datas)
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.spView.header=DefaultHeader(context)

        rootView.spView.setListener(this)
        initData()

        initListener()

        return rootView
    }

    private fun initListener() {
        adp.setOnItemClickListener { adapter, view, position ->
            when(arguments.getString("id")){
                "3"->{
                    val intent = Intent(context, RedDetailAty::class.java)
                    intent.putExtra("id",datas[position].id.toInt())
                    intent.putExtra("money",datas[position].money)
                    context.startActivity(intent)
                }
                "7"->{
                    val intent = Intent(context, ClassDetailAty::class.java)
                    intent.putExtra("id",datas[position].id)
                    intent.putExtra("lid","")
                    context.startActivity(intent)
                }
                else->{

                }
            }
        }
    }

    override fun onLoadmore() {

    }

    override fun onRefresh() {
        datas.clear()
        initData()
    }

    private fun initData() {
        OkGo.post<SpendHisBean>(Urls.BaseUrl+ Urls.SPEND_HIS)
                .params("type",arguments.getString("id"))
                .execute(object : JsonCallBack<SpendHisBean>(activity){
                    override fun onSuccess(response: Response<SpendHisBean>?) {
                        val bean = response?.body()
                        rootView.spView.onFinishFreshAndLoad()
                        if (bean?.code==1){
                            bean?.let {
                                it.data?.let {
                                    datas.addAll(bean.data)
                                    adp.notifyDataSetChanged()
                                }
                            }
                        }else{
                            ToastUtils.toast(context,bean?.msg!!)
                        }
                    }
                })
    }
}